package com.eachonline.smart.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class TradeNoGenerater {

	
	public static final String PAY_OUT_NUMBER = "PO"; // 流水号格式 支出
	
	public static final String MAIN_NUMBER = "MN"; // 流水号格式 支出
	
	private static TradeNoGenerater TradeNoGenerater = null;

	private TradeNoGenerater() {
	}

	/**
	 * 取得TradeNoGenerater的单例实现
	 * 
	 * @return
	 */
	public static TradeNoGenerater getInstance() {
		if (TradeNoGenerater == null) {
			synchronized (TradeNoGenerater.class) {
				if (TradeNoGenerater == null) {
					TradeNoGenerater = new TradeNoGenerater();
				}
			}
		}
		return TradeNoGenerater;
	}

	/**
	 * 生成支出流水号
	 */
	public synchronized String generaterPayTradeNo() {
		   String date = new SimpleDateFormat("yyMMdd").format(new Date());  
	       String seconds = new SimpleDateFormat("HHmmss").format(new Date());  
		return PAY_OUT_NUMBER+date+getTwo()+seconds+getTwo();
	}
	
	
	/**
	 * 生成主流水号
	 */
	public synchronized String generaterMainTradeNo() {
		   String date = new SimpleDateFormat("yyMMdd").format(new Date());  
	       String seconds = new SimpleDateFormat("HHmmss").format(new Date());  
		return MAIN_NUMBER+date+getTwo()+seconds+getTwo();
	}
	
	/** 
     * 产生随机的2位数 
     * @return 
     */  
    public static String getTwo(){  
        Random rad=new Random();  
  
        String result  = rad.nextInt(100) +"";  
  
        if(result.length()==1){  
            result = "0" + result;  
        }  
        return result;  
    }  
}